package xmlGenerator;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import common.Employee;

public class ParseResponse {
	private Document dom;
	private List<Employee> myEmpls = new LinkedList<Employee>();
	
	
	public ParseResponse(){
		
	}
	
	
	private void parseXmlFile(){
		//Get the factory
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

		try {

			//Using factory get an instance of document builder
			DocumentBuilder db = dbf.newDocumentBuilder();

			//Parse using builder to get DOM representation of the XML file
			dom = db.parse("src/main/resources/xml/response.xml");
			


		}catch(ParserConfigurationException pce) {
			pce.printStackTrace();
		}catch(SAXException se) {
			se.printStackTrace();
		}catch(IOException ioe) {
			ioe.printStackTrace();
		}
	}
	
	
	
	private void parseDocument(){
		//Get the root element
		Element docEle = dom.getDocumentElement();
		NodeList nl2 = docEle.getElementsByTagName("error");
		Element el2 = (Element) nl2.item(0);
		System.out.println(el2.getAttribute("code"));
		System.out.println(getTextValue(docEle, "error"));
		
		//System.out.println(docEle.getAttributeNode("error"));
		//get a nodelist of  elements
		NodeList nl = docEle.getElementsByTagName("error");
		
		
		if(nl != null && nl.getLength() > 0) {
			for(int i = 0 ; i < nl.getLength();i++) {

				//get the employee element
				Element el = (Element)nl.item(i);
				//System.out.println(el.toString());
				
			}
		}
	}
	
	/**
	 * I take an employee element and read the values in, create
	 * an Employee object and return it
	 */
	private Employee getEmployee(Element empEl) {

		//for each <employee> element get text or int values of
		//name ,id, age and name
		String name = getTextValue(empEl,"Name");
		int id = getIntValue(empEl,"Id");
		int age = getIntValue(empEl,"Age");

		String type = empEl.getAttribute("type");

		//Create a new Employee with the value read from the xml nodes
		
		Employee e = new Employee(name,id,age,type);
		
		return e;
	}


	/**
	 * I take a xml element and the tag name, look for the tag and get
	 * the text content
	 * i.e for <employee><name>John</name></employee> xml snippet if
	 * the Element points to employee node and tagName is 'name' I will return John
	 */
	private String getTextValue(Element ele, String tagName) {
		String textVal = null;
		NodeList nl = ele.getElementsByTagName(tagName);
		if(nl != null && nl.getLength() > 0) {
			Element el = (Element)nl.item(0);
			textVal = el.getFirstChild().getNodeValue();
		}

		return textVal;
	}


	/**
	 * Calls getTextValue and returns a int value
	 */
	private int getIntValue(Element ele, String tagName) {
		//in production application you would catch the exception
		return Integer.parseInt(getTextValue(ele,tagName));
	}
	
	public void printData(){
		System.out.println("Parsing the XML...");
		
		parseXmlFile();
		parseDocument();

//		System.out.println("No of Employees '" + myEmpls.size() + "'.");
//
//		Iterator<Employee> it = myEmpls.iterator();
//		while(it.hasNext()) {
//			System.out.println(it.next().toString());
//		}
		
		System.out.println("Successful");
	}
}
